//
//  WDModifyPwdViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/17.
//

#import "WDModifyPwdViewController.h"
#import "WDModifyPwdViewModel.h"
#import "WDGrabOrdersPersonInfoTextView.h"
#import "WDPersonalInfoModifyTipView.h"

@interface WDModifyPwdViewController ()
@property (nonatomic, strong) WDModifyPwdViewModel *viewModel;

/// 旧密码
@property (nonatomic, strong) WDGrabOrdersPersonInfoTextView *oldPwdView;

/// 新密码
@property (nonatomic, strong) WDGrabOrdersPersonInfoTextView *newsPwdView;

/// 确认密码
@property (nonatomic, strong) WDGrabOrdersPersonInfoTextView *submitPwdView;

@property (nonatomic, strong) UIButton *btnSubmit;

@property (nonatomic, strong) WDPersonalInfoModifyTipView *modifyTipView;


/// 旧密码
- (void)initWithOldPwdView;

/// 新密码
- (void)initWithNewsPwdView;

/// 确认密码
- (void)initWithSubmitPwdView;

@end

@implementation WDModifyPwdViewController

- (void)dealloc {
    debugMethod();
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [WDModifyPwdViewModel new];
    [_viewModel setupData];
}

- (void)initWithNavBar {
    [super initWithNavBar];
    [self initWithBackButton];
    self.navigationItem.title = self.viewModel.title;
}

- (void)initWithSubView {
    [super initWithSubView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    /// 旧密码
    [self initWithOldPwdView];

    /// 新密码
    [self initWithNewsPwdView];

    /// 确认密码
    [self initWithSubmitPwdView];
    
    [self initWithBtnSubmit];

    [self initWithModifyTipView];
}

/// 旧密码
- (void)initWithOldPwdView {
    if (!_oldPwdView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, 55);
        _oldPwdView = [[WDGrabOrdersPersonInfoTextView alloc] initWithFrame:frame];
        _oldPwdView.title = @"旧密码";
        _oldPwdView.titleFont = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        _oldPwdView.titleColor = [UIColor colorWithHexString:@"#000000"];
        _oldPwdView.placeholder = @"请输入原登录密码";
        _oldPwdView.valueFont = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        _oldPwdView.valueColor = [UIColor colorWithHexString:@"#000000"];
        _oldPwdView.textAlignment = NSTextAlignmentRight;
        [self.view addSubview:_oldPwdView];
        self.oldPwdView.valueTxtField.keyboardType = UIKeyboardTypeASCIICapable;
        self.oldPwdView.valueTxtField.secureTextEntry = YES;
        self.oldPwdView.valueTxtField.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        RAC(self.viewModel.reqModel, oldPassword) = self.oldPwdView.valueTxtField.rac_textSignal;
    }
}

/// 新密码
- (void)initWithNewsPwdView {
    if (!_newsPwdView) {
        CGRect frame = self.oldPwdView.frame;
        frame.origin.y = self.oldPwdView.bottom;
        _newsPwdView = [[WDGrabOrdersPersonInfoTextView alloc] initWithFrame:frame];
        _newsPwdView.title = @"新密码";
        _newsPwdView.titleFont = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        _newsPwdView.titleColor = [UIColor colorWithHexString:@"#000000"];
        _newsPwdView.placeholder = @"请输入8位及以上数字和字母组合";
        _newsPwdView.textAlignment = NSTextAlignmentRight;
        _newsPwdView.valueFont = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        _newsPwdView.valueColor = [UIColor colorWithHexString:@"#000000"];
        [self.view addSubview:_newsPwdView];
        self.newsPwdView.valueTxtField.keyboardType = UIKeyboardTypeASCIICapable;
        self.newsPwdView.valueTxtField.secureTextEntry = YES;
        self.newsPwdView.valueTxtField.clearButtonMode = UITextFieldViewModeWhileEditing;

        RAC(self.viewModel.reqModel, newsPassword) = self.newsPwdView.valueTxtField.rac_textSignal;
    }
}

/// 确认密码
- (void)initWithSubmitPwdView {
    if (!_submitPwdView) {
        CGRect frame = self.oldPwdView.frame;
        frame.origin.y = self.newsPwdView.bottom;
        _submitPwdView = [[WDGrabOrdersPersonInfoTextView alloc] initWithFrame:frame];
        _submitPwdView.title = @"确认密码";
        _submitPwdView.titleFont = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        _submitPwdView.titleColor = [UIColor colorWithHexString:@"#000000"];
        _submitPwdView.placeholder = @"请确认新密码";
        _submitPwdView.textAlignment = NSTextAlignmentRight;
        _submitPwdView.valueFont = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        _submitPwdView.valueColor = [UIColor colorWithHexString:@"#000000"];
        [self.view addSubview:_submitPwdView];
        self.submitPwdView.valueTxtField.keyboardType = UIKeyboardTypeASCIICapable;
        self.submitPwdView.valueTxtField.secureTextEntry = YES;
        self.submitPwdView.valueTxtField.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        RAC(self.viewModel.reqModel, newsPassword1) = self.submitPwdView.valueTxtField.rac_textSignal;
    }
}


- (void)initWithBtnSubmit {
    CGRect frame = CGRectMake(20, self.submitPwdView.bottom + 96, kScreenW - 40, 50);
    _btnSubmit = [UIButton createWithButton:self.view frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(actionWithSubmit:) propertiesBlock:^(UIButton * _Nonnull button) {
        [button setTitle:@"确认修改" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor colorWithHexString:@"#19C46E"];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
    }];
}

/// 修改密码点击确认按钮
/// - Parameter sender: sender description
- (void)actionWithSubmit:(id)sender {
    @weakify(self);
    if ([objectNull(self.viewModel.reqModel.oldPassword) isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"请输入旧密码！"];
        return;
    }
    
    if ([objectNull(self.viewModel.reqModel.newsPassword) isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"请输入新密码！"];
        return;
    }
    
    NSString *newsPassword = objectNull(self.viewModel.reqModel.newsPassword);
    if (newsPassword.length < 8 || ![EDSUtility isAlphaNumeric:newsPassword]) {
        [SVProgressHUD showInfoWithStatus:@"请输入8位及以上数字和字母组合"];
        return;
    }
    
    if ([objectNull(self.viewModel.reqModel.newsPassword1) isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"请输入确认密码！"];
        return;
    }
    
    if (![self.viewModel.reqModel.newsPassword isEqualToString:self.viewModel.reqModel.newsPassword1]) {
        [SVProgressHUD showInfoWithStatus:@"新密码跟确认密码不相同！"];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"密码修改中..."];
    [self.viewModel requestWithMoifyPwd:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        [SVProgressHUD dismiss];
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self showModifyTip: YES errTip:nil];
        } else {
//            if (![objectNull(respond.msg) isEqualToString:@""]) {
//                [SVProgressHUD showInfoWithStatus:respond.msg];
//            }
            [self showModifyTip: NO errTip:respond.msg];
        }
    }];
}

- (void)actionWithLogout {
//    @weakify(self);
    [self.viewModel requestWithLogout:^(EDSRespondDataEntity * _Nonnull respond) {
        if (respond.code == EDS_NETWORK_SUCCESS) {
//            [SVProgressHUD showSuccessWithStatus:@"退出登录成功！"];
            [[EDSAppDelegateTool sharedInstance] updateRootVC];
        } else {
//            [SVProgressHUD showInfoWithStatus:@"退出登录失败，请稍后再试！"];
        }
    }];
}

/// 显示修改后的弹窗
/// - Parameter suc: suc description
- (void)showModifyTip:(BOOL)suc errTip:(NSString *)errTip {
    @weakify(self);
    NSDictionary *params = @{
        @"desc": errTip == nil?@"您的密码修改失败" : errTip, // 您的密码已修改成功
        @"imageTipName": @"ic-error-tip" // ic-success-tip
    };
    NSString *title = @"修改失败"; // 修改成功
    if (suc) {
        params = @{
            @"desc": @"您的密码已修改成功", // 您的密码已修改成功
            @"imageTipName": @"ic-success-tip" // ic-success-tip
        };
        title = @"修改成功"; // 修改成功
    }
    [_modifyTipView showWithTitle:title data:params completion:^{
        @strongify(self);
        [self.modifyTipView hide];
        if (suc) {
            [self clickedBack:nil];
            [self actionWithLogout]; // 退出登录
        }
    } failed:^{
        @strongify(self);
        [self.modifyTipView hide];
        if (suc) {
            [self clickedBack:nil];
            [self actionWithLogout]; // 退出登录
        }
    }];
}

- (void)initWithModifyTipView {
    CGRect frame = CGRectMake((kScreenW - 296) * 0.5, (kScreenH - 160) * 0.5 - 30, 296, 160);
    _modifyTipView = [[WDPersonalInfoModifyTipView alloc] initWithFrame:frame];
}


@end
